<!DOCTYPE HTML>
<html>
<head>
    <title>Stock Chart</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0">
    <script type="text/javascript" src="/static/charting_library/charting_library.standalone.js"></script>
    <script type="text/javascript" src="/static/datafeeds/udf/dist/bundle.js"></script>
    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #tv_chart_container {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>
<body>
    <div id="tv_chart_container"></div>
    <script>
        window.onload = function() {
            try {
                const container = document.getElementById('tv_chart_container');
                if (!container) {
                    throw new Error('Chart container not found!');
                }

                // 创建UDF数据源
                const udfDatafeed = new Datafeeds.UDFCompatibleDatafeed(
                    "http://localhost:5001/api/udf",  // UDF服务器URL
                    30000,  // 更新频率：1秒
                    undefined,  // 不使用limitedServerResponse
                );

                const widget = new TradingView.widget({
                    "width": "100%",
                    "height": "100%",
                    "symbol": "{{ symbol }}",
                    "interval": "D",
                    "timezone": "Asia/Shanghai",
                    "theme": "light",
                    "style": "1",
                    "toolbar_bg": "#f1f3f6",
                    "enable_publishing": false,
                    "withdateranges": true,
                    "hide_side_toolbar": false,
                    "allow_symbol_change": true,
                    "container": "tv_chart_container",
                    
                    // 使用创建的UDF数据源
                    "datafeed": udfDatafeed,
                    "library_path": "/static/charting_library/",
                    
                    // 添加指标库配置
                    "studies_overrides": {
                        "volume.volume.color.0": "#ef535080",
                        "volume.volume.color.1": "#26a69a80"
                    },
                    
                    // 默认加载的指标
                    "studies_access": {
                        "type": "black",  // black表示除了列表中的都允许
                        "tools": [
                            { "name": "Overlay" },
                            { "name": "Compare" },
                            { "name": "Volume" }
                        ]
                    },
                    
                    // 设置语言和时区
                    "locale": "en",  // 使用中文
                    "timezone": "Asia/Shanghai",  // 设置时区
                    
                    // // 启用所有内置指标
                    // "studies_default_indicators": [
                    //     "Volume@tv-basicstudies",
                    //     "MACD@tv-basicstudies",
                    //     "RSI@tv-basicstudies",
                    //     "MASimple@tv-basicstudies"
                    // ],
                    
                    "time_frames": [
                        { text: "1D", resolution: "30", description: "1 天", title: "1D" },
                        { text: "5D", resolution: "30", description: "5 天", title: "5D" },
                        { text: "1M", resolution: "30", description: "1 月", title: "1M" },
                        { text: "3M", resolution: "30", description: "3 月", title: "3M" },
                        { text: "6M", resolution: "30", description: "6 月", title: "6M" },
                    ],
                    "overrides": {
                        "timeScale.rightOffset": 5,
                        "timeScale.barSpacing": 10,
                    },
                    "disabled_features": [
                        "header_symbol_search"  // 只禁用顶部的股票搜索
                    ],
                    "enabled_features": [
                        "use_localstorage_for_settings",
                        "timeframes_toolbar"                    
                    ],
                    "debug": true,
                    "fullscreen": true,
                    "autosize": true,
                
                    "charts_storage_url": 'http://localhost:5001/api',
                    "charts_storage_api_version": "1.1",
                    "client_id": "thingxs.com",
                    "user_id": "thingxs.com",
                    
                    // 启用保存/加载功能
                    "save_load": true,                // 启用保存/加载功能
                    "auto_save_delay": 5,             // 自动保存延迟（秒）
                    "load_last_chart": true,          // 加载最后保存的图表
                   
                });

                // 添加错误处理
                widget.onChartReady(() => {
                    console.log('Chart is ready');
                });
            } catch (error) {
                console.error('Error initializing chart:', error);
            }
        };
    </script>
</body>
</html> 